/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package common;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

/**
 *
 * @author Administrator
 */

public class NomeValidator implements Validator{
  public void validate(FacesContext context, UIComponent component, Object nome) throws ValidatorException {

    if(!(nome instanceof String)){
        FacesMessage message = new FacesMessage();
        message.setSummary("* Errore irreversibile");
        throw new ValidatorException(message);
    }
    String name = (String) nome;
    //verifico che non sia vuoto
    if(name.length()==0){
        FacesMessage message = new FacesMessage();
        message.setSummary("* Devi inserire il tuo Nome");
        throw new ValidatorException(message);
    }
    //verifico che sia superiore a 3
    if (name.length() < 2){
        FacesMessage message = new FacesMessage();
        message.setSummary("* Il nome deve avere almeno 3 caratteri");
        throw new ValidatorException(message);
    }
  }
}
